package com.copm.demonstrationsystem.service.introductionfeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.config.SystemConfig;
import com.copm.demonstrationsystem.dao.commons.ImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionTextDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.MainPageDao;
import com.copm.demonstrationsystem.dto.introductionfeatures.MainPageDto;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionText;
import com.copm.demonstrationsystem.pojo.introductionfeatures.MainPage;
import com.copm.demonstrationsystem.service.introductionfeatures.MainPageService;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.MultiImageAndTextHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.SinglonImageHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.TransformImagePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 18:49 2018/4/23
 */
@Service
public class MainPageServiceImpl implements MainPageService {

    private static Logger logger = LoggerFactory.getLogger(MainPageServiceImpl.class);

    @Autowired
    private SystemConfig config;

    @Autowired
    MainPageDao mainPageDao;

    @Autowired
    IntroductionTextDao introductionTextDao;

    @Autowired
    MultiImageAndTextHandle multiImageAndTextHandle;

    @Autowired
    SinglonImageHandle singlonImageHandle;

    @Autowired
    TransformImagePath transformImagePath;

    @Autowired
    ImageDao imageDao;

    //获取“首页”页面信息（全为图片）
    @Override
    public ResponseStatus getPage() {
        ResponseStatus status = new ResponseStatus();
        //Object[] obj = new Object[2];
        MainPageDto dto = new MainPageDto();
        //获取页面文本
//        List<IntroductionText> texts = introductionTextDao.findByTextKind(7L);
        //获取页面对象（背景和项目图片）
        MainPage mainPage = mainPageDao.findPage();
        //向对象数组添加页面文本和图片对象
//        obj[0] = texts;
//        obj[1] = mainPage;
        if (null == mainPage) {
            status.setStatue(0);
            status.setData(" ");
            logger.info("查询无页面！");
            return status;
        }

        Image projectImage = mainPage.getProjectImage();
        if (null == projectImage) {
            mainPage.setProjectImage(null);
        } else {
            transformImagePath.transformImagePath(projectImage);
        }
        Image backgroundImage = mainPage.getBackgroundImage();
        if (null == backgroundImage) {
            mainPage.setBackgroundImage(null);
        } else {
            transformImagePath.transformImagePath(backgroundImage);
        }
        dto.setBackgroundImage(mainPage.getBackgroundImage());
        dto.setProjectImage(mainPage.getProjectImage());
        //       dto.setTitles(texts);

        status.setStatue(1);
        status.setData(dto);
        logger.info(" 首页 页面查询成功！");
        return status;
    }

    //添加“首页”页面背景图片
    @Override
    public ResponseStatus addBackgroundImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);
        //获取“主页主页”页面对象
        MainPage mainPage = mainPageDao.findPage();
        //设置“主页”页面图片
        mainPage.setBackgroundImage(image);
        //保存页面对象
        mainPageDao.save(mainPage);
        status.setStatue(1);
        logger.info(" 首页 背景图片添加成功！");
        return status;
    }

    //添加“首页”页面项目图片
    @Override
    public ResponseStatus addProjectImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);
        //获取“主页”页面对象
        MainPage mainPage = mainPageDao.findPage();
        //设置“主页”页面图片
        mainPage.setProjectImage(image);
        //保存野蛮对象
        mainPageDao.save(mainPage);
        status.setStatue(1);
        logger.info(" 首页 页面项目图片添加成功！");
        return status;
    }

    //删除“首页”页面背景图片
    @Override
    public ResponseStatus deleteBackgroundImage() {
        ResponseStatus status;
        //获取页面对象
        MainPage mainPage = mainPageDao.findPage();
        //获取图片名
        Image image = mainPage.getBackgroundImage();
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“主页”页面 ，设置图片为空
        mainPage.setBackgroundImage(null);
        //保存已删除的页面对象
        mainPageDao.save(mainPage);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info(" 首页页面 背景图片删除成功！");
        return status;
    }

    //删除“首页”页面项目图片
    @Override
    public ResponseStatus deleteProjectImage() {
        ResponseStatus status;
        //获取页面对象
        MainPage mainPage = mainPageDao.findPage();
        //获取图片名
        Image image = mainPage.getProjectImage();
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“主页”页面 ，设置图片为空
        mainPage.setProjectImage(null);
        //保存已删除的页面对象
        mainPageDao.save(mainPage);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info(" 首页页面 项目图片删除成功！");
        return status;
    }

    //修改“首页”页面背景图片
    @Override
    public ResponseStatus modifyBackgroundImage(MultipartFile file) {
        ResponseStatus status = new ResponseStatus();
        //获取页面对象
        MainPage mainPage = mainPageDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        if (null == mainPage) {
            //实例化“项目介绍”页面对象
            mainPage = new MainPage();
            //保存对象
            mainPageDao.save(mainPage);
        }
        if (null != mainPage.getBackgroundImage()) {
            status = deleteBackgroundImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addBackgroundImage(file);
        logger.info(" 首页页面 背景图片修改成功！");
        return status;
    }

    //修改“首页”页面项目图片
    @Override
    public ResponseStatus modifyProjectImage(MultipartFile file) {
        ResponseStatus status = new ResponseStatus();
        //获取页面对象
        MainPage mainPage = mainPageDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        if (null == mainPage) {
            //实例化“主页”页面对象
            mainPage = new MainPage();
            //保存对象
            mainPageDao.save(mainPage);
        }
        if (null != mainPage.getProjectImage()) {
            status = deleteProjectImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addProjectImage(file);
        logger.info(" 首页页面 项目图片图片修改成功！");

        return status;
    }

//    //删除“首页”页面模块标签文本
//    @Override
//    public ResponseStatus deleteMainPageText(Long id) {
//        ResponseStatus status = multiImageAndTextHandle.deleteIntroductionText(id, 7L);
//        logger.info("删除“首页”页面模块标签文本成功！");
//        return status;
//    }
//
//    //添加“首页”页面模块标签文本
//    @Override
//    public ResponseStatus addMainPageText(String title) {
//        ResponseStatus status = multiImageAndTextHandle.addIntroductionText(title, 7L);
//        logger.info("添加“首页”页面模块标签文本成功！");
//        return status;
//    }
//
//    //修改“首页”页面模块标签文本
//    @Override
//    public ResponseStatus modifyMainPageText(Long id, String title) {
//        //删除待修改的原标签文本
//        ResponseStatus status = deleteMainPageText(id);
//        if (0 == status.getStatue()) {
//            return status;
//        }
//        //添加待修改的标签文本
//        status = addMainPageText(title);
//        if (0 == status.getStatue()) {
//            return status;
//        }
//        logger.info("修改“首页”页面模块标签文本成功");
//        return status;
//    }
}
